package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.RecyclerViewBaseAdapter;
import com.example.zzl.custom.MyTag;
import com.example.zzl.databinding.HomeItemivRvBinding;
import com.example.zzl.model.bean.Home.BrandExclusive;
import com.example.zzl.utils.specification.VolleySingleton;
import com.example.zzl.viewmodel.HomeFragment.RecommendPresenter;

import java.util.List;

/**
 * Created by ligg on 2017/2/9.
 */

public class RecommendRvAdapter extends RecyclerViewBaseAdapter<BrandExclusive.ResultBean, RecommendRvAdapter.ViewHolder>{

    RecommendPresenter callback;

    public RecommendRvAdapter(Context context, List<BrandExclusive.ResultBean> list, RecommendPresenter callback) {
        super(context, list);
        this.callback = callback;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        HomeItemivRvBinding db = DataBindingUtil.inflate(inflater, R.layout.home_itemiv_rv, parent, false);
        ViewHolder viewHolder = new ViewHolder(db.getRoot());
        viewHolder.db = db;
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        BrandExclusive.ResultBean resultBean = list.get(position);
        if (holder.ivAdapter == null) {
            holder.db.setCallback(callback);
            holder.db.rv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            holder.db.rv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL));
            holder.list = resultBean.listRecommendInfo;
            holder.ivAdapter = new RecommendIvRvAdapter(context, holder.list, position);
            holder.ivAdapter.setOnItemClickLitener(callback);
            holder.db.rv.setAdapter(holder.ivAdapter);
        } else {
            holder.ivAdapter.itemPosition = position;
            holder.list.clear();
            holder.list.addAll(resultBean.listRecommendInfo);
            holder.ivAdapter.notifyDataSetChanged();
        }
        holder.db.rv.setTag(position);
        final ImageView iv = holder.db.iv;
        iv.setImageBitmap(null);
        iv.setTag(new MyTag(resultBean.strBrandImage, position));
        VolleySingleton.volleySingleton.getImageLoader().get(resultBean.strBrandImage, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (((MyTag) iv.getTag()).tag.equals(response.getRequestUrl())) {
                    iv.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }

   /* private void setCarouselVp(final ViewGroup parent, final ViewPager vp) {
        vp.setOffscreenPageLimit(3);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (parent != null) {
                    parent.invalidate();
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        parent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return vp.dispatchTouchEvent(event);
            }
        });
    }*/

    class ViewHolder extends RecyclerView.ViewHolder {

        private HomeItemivRvBinding db;
        private RecommendIvRvAdapter ivAdapter;
        private List<BrandExclusive.ResultBean.ListRecommendInfoBean> list;


        public ViewHolder(View itemView) {
            super(itemView);
        }


    }
}
